package cn.marinda.resturants.service.impl;

import cn.marinda.resturants.entity.Administrator;
import cn.marinda.resturants.entity.Identity;
import cn.marinda.resturants.entity.User;
import cn.marinda.resturants.global.Pages;
import cn.marinda.resturants.mapper.AdministratorMapper;
import cn.marinda.resturants.service.AdministratorService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/** Administrator 管理员 Service 服务层实现类
 *
 * @date 2021/12/2
 * @version 1.0
 * @author  Marinda
 *
 */
@Service("administratorService")
public class AdministratorServiceImpl  extends ServiceImpl<AdministratorMapper,Administrator>  implements AdministratorService {


    /**
     * 分页
     *
     * @param pages 分页
     * @return 分页结果
     */
    @Override
    public IPage<Administrator> selectAdministratorPages(Pages<?> pages) {
        return baseMapper.selectAdministratorPages(pages);
    }

    /**
     * 查询所有Administrator
     *
     * @return 所有Administrator
     */
    @Override
    public List<Administrator> queryList() {
        return baseMapper.selectList(null);
    }

    /**
     * 添加一个管理员信息
     *
     * @param administrator 管理员信息
     * @return 结果
     */
    @Override
    public boolean addAdministrator(Administrator administrator) {
        administrator.setUid(UUID.randomUUID().toString());
        return baseMapper.insert(administrator)>0;
    }

    /**
     * 删除一个管理员信息
     *
     * @param uid 管理员
     * @return 结果
     */
    @Override
    public boolean deleteAdministrator(String uid) {
        return baseMapper.deleteById(uid)>0;
    }

    /**
     * 通过uid来查询一个用户
     *
     * @param uid 用户uid
     * @return 用户
     */
    @Override
    public Administrator selectAdministratorid(String uid) {
        return baseMapper.selectById(uid);
    }

    /**
     * 查询一个身份类型下的所有管理员
     *
     * @param type 类型
     * @return 包含administrator 的一个集合
     */
    @Override
    public List<Administrator> selectTypeAdmin(String type) {
        return baseMapper.selectTypeAdmin(type);
    }

    /**
     * 通过查询管理员名字找到这个Admin
     *
     * @param name adminName
     * @return Administrator
     */
    @Override
    public Administrator selectAdminName(String name) {
        return baseMapper.selectAdminName(name);
    }

    /**
     * 通过查询Administrator的Phone能找到这个Administator
     *
     * @param phone admin phone
     * @return Administrator
     */
    @Override
    public Administrator selectPhone(String phone) {
        return baseMapper.selectPhone(phone);
    }

    /**
     * 乐观锁修改管理员细腻些
     *
     * @param administrator 管理员
     * @return 结果
     */
    @Override
    public boolean updateAdministrator(Administrator administrator) {
        Administrator sqlAdministrator = baseMapper.selectById(administrator.getUid());
        if(sqlAdministrator == null){
            return false;
        }
        administrator.setVersion(sqlAdministrator.getVersion());
        return lambdaUpdate().eq(Administrator::getUid,administrator.getUid()).eq(Administrator::getVersion,administrator.getVersion()).set(Administrator::getVersion,administrator.getVersion()+1).update(administrator);
    }
}
